package com.tianqu.tms.www.car.controller;

import java.util.ArrayList;
import java.util.List;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferProcessHistory;
import com.tianqu.tms.www.car.viewhelper.CarTransferViewHelper;

/**
 * @author mufeiyu
 * 高级查询过户记录页controller
 */
public class CarSearchTransferRecordInfoController extends TmsController{
	private Logger logger = Logger.getLogger(CarSearchTransferRecordInfoController.class);
	public final static String CARTRANSFERVIEWHELPER = "CarTransferViewHelper";
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			String basicCarId = (String)uc.getAttribute("serial");
			
			CarTransferViewHelper viewHelper = new CarTransferViewHelper();
			Car car = new Car();
			List<TransferAcceptedInfo> acceptedList = new ArrayList<TransferAcceptedInfo>();
			TransferAcceptedInfo acceptedInfo = new TransferAcceptedInfo();
			TransferProcessHistory processHistory = new TransferProcessHistory();			
			
			car = car.findCarBySerial(basicCarId);
			acceptedList = acceptedInfo.findTransferAssessInfoByVin(car.getVin());
			StringBuffer str = new StringBuffer();
			if(acceptedList.size() > 1){
				// 车辆流水号
				String serial = (String)uc.getParameter("serialStr");
				// 插入时间
				String insertTimeS = (String)uc.getParameter("insertTimeS");
				for(int i = 0;i<acceptedList.size();i++){
					TransferAcceptedInfo info = acceptedList.get(i);
					if(!StringUtil.isNullOrBlank(info.getInsertTimeS())){
						if(StringUtil.isNullOrBlank(insertTimeS)){
							if(i == 0){
								str.append("<a href=\"carSearchTransferRecordInfo.tq?serialStr=" + info.getSerial() + "&insertTimeS=" + info.getInsertTimeS() + "\" class=\"action-current\">" + info.getInsertTimeS() + "</a> |");
							}else {
								str.append("<a href=\"carSearchTransferRecordInfo.tq?serialStr=" + info.getSerial() + "&insertTimeS=" + info.getInsertTimeS() + "\">" + info.getInsertTimeS() + "</a> |");
							}
						}else{
							if(insertTimeS.equals(info.getInsertTimeS())){
								str.append("<a href=\"carSearchTransferRecordInfo.tq?serialStr=" + info.getSerial() + "&insertTimeS=" + info.getInsertTimeS() + "\" class=\"action-current\">" + info.getInsertTimeS() + "</a> |");
							}else {
								str.append("<a href=\"carSearchTransferRecordInfo.tq?serialStr=" + info.getSerial() + "&insertTimeS=" + info.getInsertTimeS() + "\">" + info.getInsertTimeS() + "</a> |");
							}
						}
					}					
				}
				viewHelper.setHistoryPage(str.toString());
				List list = null;
				if(StringUtil.isNullOrBlank(serial)){
					list = processHistory.loadtHistoryBySerialInsertTime(acceptedList.get(0).getSerial(),acceptedList.get(0).getInsertTimeS());
				}else {
					list = processHistory.loadtHistoryBySerialInsertTime(serial,insertTimeS);
				}
				if(list != null && list.size()>0){
					viewHelper.setTransferProcessList(list);
				}		
			}else if(acceptedList.size() == 1){
				List list = processHistory.loadtHistoryBySerial(acceptedList.get(0).getSerial());
				viewHelper.setTransferProcessList(list);			
			}else {
				List list = new ArrayList();
				viewHelper.setTransferProcessList(list);
			}
			//
			uc.setAttribute(CARTRANSFERVIEWHELPER, viewHelper);
			
			controller.commit();
			
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
		
	}
}
